package com.tedu.springboot2302.controller;

import com.tedu.springboot2302.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@Controller
public class UserController {

      private static File userDir;
      static{
          userDir=new File("./users");
          if(!userDir.exists()){
              userDir.mkdir();
          }
      }

      @RequestMapping("/regUser")
      public void reg(HttpServletRequest request, HttpServletResponse response) {
          //HttpServleRequest称为请求对象,内容表达的是浏览器发送过来的所有数据
          //HttpServleResponse称为响应对象,内容表达的是我们处理完业务后给浏览器回复的内容
          System.out.println("开始处理用户注册!!!!!");
          String username = request.getParameter("username");
          String password = request.getParameter("password");
          String nickname = request.getParameter("nickname");
          String ageStr = request.getParameter("age");
          System.out.println(username+","+password+","+nickname+","+ageStr);
          if(username==null||username.isEmpty()||
                  password==null||password.isEmpty()||
                  nickname==null||nickname.isEmpty()||
                  ageStr==null||ageStr.isEmpty()||
                  !ageStr.matches("[0-9]+")){
              try {
                  response.sendRedirect("/reg_info_error.html");
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return;
          }

          int age =Integer.parseInt(ageStr);
          User user =new User(username,password,nickname,age);
          //File file = new File("./users/"+username+".obj");
          File file = new File(userDir,username+".obj");
          //第一个参数的File对象表示单签文件在那个目录中
          if(file.exists()){
              try {
                  response.sendRedirect("/haveuser.html");
              } catch (IOException e) {
                  e.printStackTrace();
              }
              return;
          }
          ObjectOutputStream oos = null;
          try {
              FileOutputStream fos = new FileOutputStream(file);
              oos = new ObjectOutputStream(fos);
              oos.writeObject(user);
              response.sendRedirect("/reg_success.html");
          } catch (IOException e) {
              e.printStackTrace();
          }finally {
              try {
                  oos.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request,HttpServletResponse response) throws IOException, ClassNotFoundException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username == null || username.isEmpty() ||
                password == null || password.isEmpty()) {
            try {
                response.sendRedirect("/login_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        File file = new File(userDir,username+".obj");
        if(file.exists()){
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            User user = (User)ois.readObject();
            if(user.getPassword().equals(password)){
                response.sendRedirect("/login_success.html");
            }else{
                response.sendRedirect("/login_fail.html");
            }

        }else{
            response.sendRedirect("/login_fail.html");
        }
    }


    @RequestMapping("/userList")
    public void userList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理显示用户列表");
        List<User> list = new ArrayList<>();
        File[] file =userDir.listFiles(f->f.getName().endsWith(".obj"));
        for(File sub: file){
            try (
                    FileInputStream fis = new FileInputStream(sub);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    ){
                User user= null;
                try {
                    user = (User)ois.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                list.add(user);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            //在获取输出流前设置字符集
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户列表</h1>");

            pw.println("<table border='1'>");

            pw.println("<tr>");
            pw.println("<td>用户名</td>");
            pw.println("<td>密码</td>");
            pw.println("<td>昵称</td>");
            pw.println("<td>年龄</td>");
            pw.println("</tr>");

            for(User user : list) {
                pw.println("<tr>");
                pw.println("<td>" + user.getUsername()  + "</td>");
                pw.println("<td>" + user.getPassword()  + "</td>");
                pw.println("<td>" + user.getNickname()  + "</td>");
                pw.println("<td>" + user.getAge()  + "</td>");
                pw.println("</tr>");
            }

            pw.println("</table>");

            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");

        } catch (IOException e) {
            e.printStackTrace();
        }


    }























}
